package com.bilibili.juc.cf;

import java.util.concurrent.*;

/**
 * @auther zzyy
 * @create 2022-01-17 16:36
 */
public class CompletableFutureAPI2_1Demo
{
    public static void main(String[] args)
    {
        ExecutorService threadPool = Executors.newFixedThreadPool(3);

        CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            //暂停几秒钟线程
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("111");
            return "1";
        }, threadPool).handle((f, e) -> {
//            int i = 10 / 0;
            System.out.println("222");
            return f + "->" + 2;
        }).handle((f, e) -> {
            System.out.println("333");
            return f + "->" + 3;
        }).handle((f, e) -> {
            System.out.println("444");
            return f + "->" + 4;
        }).whenComplete((v, e) -> {
            if (e == null) {
                System.out.println("----计算结果： " + v);
            }
        }).exceptionally(e -> {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return null;
        });

        System.out.println(Thread.currentThread().getName()+"----主线程先去忙其它任务");

        try {
            System.out.println("最后的结果：" + completableFuture.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        threadPool.shutdown();
    }
}
/*
main----主线程先去忙其它任务
111
222
333
444
----计算结果： 1->2->3->4
最后的结果：1->2->3->4
* */
